home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 68 / 68.xpi / chrome / dictionarysearch.jar / content / dictionarysearchOverlay.js < prev    next >
Text File  |  2009-12-05  |  9KB  |  240 lines

  1. /* -*- Mode: js; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
  2. // $Id: dictionarysearchOverlay.js,v 1.23 2009/12/05 18:38:15 jaap Exp $
  3.  
  4. /* ***** BEGIN LICENSE BLOCK ***** 
  5.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6.  *
  7.  * The contents of this file are subject to the Mozilla Public License Version
  8.  * 1.1 (the "License"); you may not use this file except in compliance with
  9.  * the License. You may obtain a copy of the License at
  10.  * http://www.mozilla.org/MPL/
  11.  *
  12.  * Software distributed under the License is distributed on an "AS IS" basis,
  13.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14.  * for the specific language governing rights and limitations under the
  15.  * License.
  16.  *
  17.  * The Initial Developer of the Original Code is Jaap Haitsma.
  18.  * Portions created by the Initial Developer are Copyright (C) 2003
  19.  * by the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Jaap Haitsma <jaap@haitsma.org>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36. if(!org) var org={};
  37. if(!org.mozdev) org.mozdev={};
  38. if(!org.mozdev.dictionarysearch) org.mozdev.dictionarysearch={};
  39.  
  40.  
  41. org.mozdev.dictionarysearch = {
  42. DEBUG: false,
  43. NUM_DICTIONARIES: 4,
  44. stringBundle: null,
  45. prefs: null,
  46. APP_NAME: "DictionarySearch",
  47. VERSION: "3.6.1",
  48.  
  49. dumpObject: function(obj) {
  50.     for(i in obj){         
  51.         this.debug(i + " = " + obj[i] + "\n");
  52.     }
  53. },
  54.  
  55. debug: function(str) {
  56.     if (!this.DEBUG) {
  57.         return;
  58.     }
  59.     consoleService = Components.classes["@mozilla.org/consoleservice;1"]
  60.         .getService(Components.interfaces.nsIConsoleService);
  61.     consoleService.logStringMessage(this.APP_NAME + ": " + str);
  62. },
  63.  
  64. init: function () {
  65.     this.debug("init()");
  66.  
  67.     this.prefs = Components.classes["@mozilla.org/preferences-service;1"].
  68.                             getService(Components.interfaces.nsIPrefService).getBranch("dictionarysearch.");
  69.  
  70.     this.stringBundle = document.getElementById("dictionarysearch_string_bundle");
  71.  
  72.     if (!this.prefs.prefHasUserValue("menutext1")){
  73.         this.debug ("init(): no preferences found in prefs.js taking defaults");
  74.         // Assign default values
  75.         this.prefs.setCharPref("url1", this.stringBundle.getString("dictionarysearch.default.URL"));
  76.         this.prefs.setCharPref("menutext1", this.stringBundle.getString("dictionarysearch.default.menutext"));
  77.     }
  78.     for (i = 2; i <= this.NUM_DICTIONARIES; i++) {
  79.         if (!this.prefs.prefHasUserValue("menutext"+i)) {
  80.             this.prefs.setCharPref("url"+i, "");
  81.             this.prefs.setCharPref("menutext"+i, "");
  82.         }
  83.     }            
  84.     if (document.getElementById("contentAreaContextMenu")) {
  85.         document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", function () {org.mozdev.dictionarysearch.popup()}, false);
  86.     }
  87.  
  88.     if (!this.prefs.prefHasUserValue("version")) {
  89.        this.debug("init(): no version found");
  90.        setTimeout(function() { window.openUILinkIn("http://dictionarysearch.mozdev.org/thanks.html", "tab"); }, 500);
  91.        this.prefs.setCharPref("version", this.VERSION);
  92.     }
  93.     if (this.prefs.getCharPref("version") != this.VERSION) {
  94.         this.debug("init(): newer version");
  95.         setTimeout(function() { window.openUILinkIn("http://dictionarysearch.mozdev.org/thanks.html", "tab"); }, 500);
  96.         this.prefs.setCharPref("version", this.VERSION);
  97.     }
  98.  
  99.  
  100. },
  101.  
  102. popup: function() { 
  103.     // hide the Dictionary Search item when apropriate (use same logic as for web search)
  104.     this.debug("popup" + this.prefs);
  105.     var selectedText = this.getSelectedText(" ");
  106.        
  107.     if (selectedText!=""){
  108.         this.debug("popup() text selected");
  109.         if (selectedText.length > 18){
  110.             selectedText = selectedText.substr(0,14) + "...";
  111.         }
  112.         var menuText;
  113.         var item;
  114.  
  115.         for (i=1 ;i<=this.NUM_DICTIONARIES; i++){
  116.             menuText = this.prefs.getCharPref("menutext"+i);
  117.             item = document.getElementById("context_dictionarysearchselect"+i);
  118.             item.hidden = true;
  119.             if (menuText!=""){        
  120.                 item.hidden = false;        
  121.                 menuText = menuText.split("$");
  122.                 menuText = menuText[0] + selectedText + menuText[1];
  123.                 item.setAttribute("label", menuText);
  124.             }
  125.         }
  126.     }
  127.     else{
  128.  
  129.        for (i=1 ;i<=this.NUM_DICTIONARIES; i++){
  130.             item = document.getElementById("context_dictionarysearchselect"+i);
  131.             item.hidden = true;
  132.        }
  133.     }
  134. },
  135.  
  136. search: function(n){ 
  137.     var url = this.prefs.getCharPref("url"+n);
  138.     url = url.split("$");
  139.     url = url[0] + this.getSelectedText("+").toLocaleLowerCase() + url[1];
  140.     this.debug ("search() : url = " + url);
  141.  
  142.     openNewTabWith(url, null, null, true);
  143. },
  144.  
  145.  
  146. getSelectedText: function(concationationChar){
  147.     this.debug("getSelectedText()");
  148.     
  149.     var node = document.popupNode;
  150.     var selection = "";
  151.  
  152.     if ((node instanceof HTMLTextAreaElement) || (node instanceof HTMLInputElement && node.type == "text")) {
  153.         selection = node.value.substring(node.selectionStart, node.selectionEnd);
  154.     } 
  155.     else {
  156.         var focusedWindow = new XPCNativeWrapper(document.commandDispatcher.focusedWindow, 'document', 'getSelection()');
  157.         selection = focusedWindow.getSelection().toString();
  158.     }
  159.  
  160.     // Limit length to 150 to optimize performance. Longer does not make sense
  161.     if (selection.length>=150){
  162.         selection = selection.substring(0, 149);
  163.     }
  164.     selection = selection.replace(/(\n|\r|\t)+/g, " ");
  165.     // Strip spaces at start and end.
  166.     selection = selection.replace(/(^\s+)|(\s+$)/g, "");
  167.  
  168.     selection = selection.split(" ");
  169.    
  170.     // Remove certain characters at the beginning and end of every word
  171.     for (i=0; i<selection.length; i++){
  172.         selection[i]=selection[i].replace(/^(\&|\(|\)|\[|\]|\{|\}|"|,|\.|!|\?|'|:|;)+/, "");
  173.         selection[i]=selection[i].replace(/(\&|\(|\)|\[|\]|\{|\}|"|,|\.|!|\?|'|:|;)+$/, "");
  174.     }   
  175.     selection = selection.join(concationationChar);   
  176.     return selection; 
  177. },
  178.  
  179. saveSettings: function(){    
  180.     this.debug("saveSettings()");
  181.  
  182.     var url;
  183.     var menuText;
  184.     var accessKey;
  185.     for (i=1; i<=this.NUM_DICTIONARIES; i++){
  186.         url = document.getElementById("dictionarysearchURL"+i).value;
  187.         menuText = document.getElementById("dictionarysearchMenuText"+i).value;
  188.         if (menuText != ""){       
  189.             if (!this.checkDollarSign(menuText)){
  190.                 alert(this.stringBundle.getString("dictionarysearch.menuText.error1")  + " " + i
  191.                       + " " + this.stringBundle.getString("dictionarysearch.menuText.error2"));
  192.                 return false;
  193.             }
  194.  
  195.             if (!this.checkDollarSign(url)){
  196.                 alert(this.stringBundle.getString("dictionarysearch.URL.error1") + " " + i
  197.                   + " " + this.stringBundle.getString("dictionarysearch.URL.error2"));
  198.                 return false;
  199.             }
  200.  
  201.         }
  202.         this.prefs.setCharPref("url"+i, url);
  203.         this.prefs.setCharPref("menutext"+i, menuText);
  204.     }
  205.     return true;
  206. },
  207.  
  208. loadSettings: function(){
  209.     this.debug("loadSettings()");
  210.     // We need to set the prefs and string bundle again, because we get another instance of the object in the pref dialog :-(
  211.     this.prefs = Components.classes["@mozilla.org/preferences-service;1"].
  212.                             getService(Components.interfaces.nsIPrefService).getBranch("dictionarysearch.");
  213.  
  214.     this.stringBundle = document.getElementById("dictionarysearch_string_bundle");
  215.  
  216.  
  217.     for (i=1; i<=this.NUM_DICTIONARIES; i++){
  218.         document.getElementById("dictionarysearchURL"+i).value = this.prefs.getCharPref("url"+i);
  219.         document.getElementById("dictionarysearchMenuText"+i).value = this.prefs.getCharPref("menutext"+i);
  220.     }
  221. },
  222.  
  223. checkDollarSign: function(str){    
  224.     this.debug("checkDollarSign(str)");
  225.  
  226.     ind = str.indexOf('$');
  227.     if (ind == -1){
  228.         return false;
  229.     }
  230.     if (ind != str.lastIndexOf('$')){
  231.         return false;
  232.     }
  233.     return true;
  234. }
  235. };
  236.  
  237. // Every time a new browser window is made init will be called
  238. window.addEventListener("load", function() {org.mozdev.dictionarysearch.init()},false);
  239.  
  240.